#include "QuickSearch.hpp"
#include "../util/SpecialCharsRemover.hpp"

void QuickSearch::disableSoundex() {useSoundex=false;}
void QuickSearch::enableSoundex() {useSoundex=true;}

void QuickSearch::search(QString t) {
    text=t;
    query="SELECT track.title, artist.name AS artist, album.title AS album, directory.path, track.filename, bitrate, duration FROM album, artist, track, disc, directory WHERE track.artist=artist.idArtist AND track.disc=disc.idDisc AND disc.album=album.idAlbum AND track.iddir=directory.iddir AND ";
    QString textQuery=(useSoundex)?"SOUNDEX(":"";
    if(fields&USE_FIELD_TITLE)  textQuery+="' '||track.title";
    if(fields&USE_FIELD_ARTIST) textQuery+="' '||artist.name";
    if(fields&USE_FIELD_ALBUM)  textQuery+="' '||album.title";
    //if(fields&USE_FIELD_COMMENT)textQuery+="' '||track.comment";
    //if(fields&USE_FIELD_GENRE)  textQuery+="' '||";
    if(fields&USE_FIELD_TAG)    textQuery+="' '||tag.name";
    if(fields&USE_FIELD_FILENAME)    textQuery+="' '||track.filename";

    ///TODO: process seach text to remove special chars and explode spaces for LIKE(a,b,c)
    t=SpecialCharsRemover().removeSpecialChars(t);//process special chars (accent, ...)
    textQuery+=(useSoundex)?")":"";
}

void QuickSearch::getQuery() {
    ///TODO: perform query
    ///TODO: get & return results
}

void QuickSearch::setFields(char f) {fields=f;}

//constructors
QuickSearch::QuickSearch(): useSoundex(false), fields(USE_FIELD_TITLE|USE_FIELD_ARTIST|USE_FIELD_FILENAME) {text="";}
QuickSearch::QuickSearch(QString searchText): useSoundex(false), fields(USE_FIELD_TITLE|USE_FIELD_ARTIST|USE_FIELD_FILENAME) {text=searchText;}
QuickSearch::QuickSearch(QString searchText, char fields): useSoundex(false), fields(USE_FIELD_TITLE|USE_FIELD_ARTIST|USE_FIELD_FILENAME) {text=searchText;}
